﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.Objects.DataClasses;
using System.Data;

namespace HXDNB.DAL
{
    public class BaseDAL<T>
    {

        #region 私有方法
        //数据实体模型
        public readonly Entities _e = new Entities();
        //表名
        private string _entitySetName = null;
        //构造函数
        public BaseDAL(string entitySetName)
        {
            _entitySetName = entitySetName;
        }
        /// <summary>
        /// 获取ID主键值
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private int GetID(object model)
        {
            int value = Convert.ToInt32((model.GetType().GetProperty("ID") ?? model.GetType().GetProperty("Id") ?? model.GetType().GetProperty("id")).GetValue(model, null));
            return value;
        }

        #endregion

        /// <summary>
        /// 根据KEY查询
        /// </summary>
        public T GetByKey(int id)
        {
            return _e.ExecuteStoreQuery<T>("select * from " + _entitySetName + " where id={0}", id).FirstOrDefault();
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        public int insert(object model)
        {
            string err = null; int ID = 0;
            try
            {
                _e.AddObject(_entitySetName, model);
                _e.SaveChanges();
                ID = GetID(model);
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            finally
            {
                string msg = string.Format("添加数据[ID:{0},表:{1}]", ID, _entitySetName);
                new LogBLL().Add(LogType.添加日志, msg, ID > 0, err);
            }
            return ID;//添加成功返回自增ID
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        public bool update(EntityObject model)
        {
            string err = null; bool result = false;
            int ID = GetID(model);
            try
            {
                _e.AttachTo(_entitySetName, model);
                _e.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                result = _e.SaveChanges() > 0;
                if (result == false)
                {
                    err = "更新数目为0";
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            finally
            {
                string msg = string.Format("更新数据[ID:{0},表:{1}]", ID, _entitySetName);
                new LogBLL().Add(LogType.更新日志, msg, result, err);
            }
            return result;
        }

        /// <summary>
        /// 根据ID删除数据
        /// </summary>
        public bool Delete(int ID)
        {
            string err = null; bool result = false;
            try
            {
                var info = GetByKey(ID);
                _e.AttachTo(_entitySetName, info);
                _e.DeleteObject(info);
                result = _e.SaveChanges() > 0;
                if (result == false)
                {
                    err = "删除数目为0";
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            finally
            {
                string msg = string.Format("删除数据[ID:{0},表:{1}]", ID, _entitySetName);
                new LogBLL().Add(LogType.删除日志, msg, result, err);
            }
            return result;
        }
    }
}
